package com.segment.ranking.control;

import java.text.DateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;

import javax.servlet.http.HttpServletRequest;


import net.sf.json.JSONArray;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import com.segment.ranking.service.RankingService;
import com.segment.ranking.vo.RankingVO;



@Controller
public class RankingControl {
	
	private static final Logger logger = LoggerFactory.getLogger(RankingControl.class);
	
	@RequestMapping(value = "/test/")
	public ModelAndView test(Locale locale, HttpServletRequest request, Model model) {
		logger.info("Test.", locale);

		String userCode = request.getParameter("userCode");
		if(userCode == null){
			userCode = "";
		}
		
		int score = 0;
		if(request.getParameter("score") != null){
			score = Integer.parseInt(request.getParameter("score"));
		}
		
		RankingVO vo = new RankingVO();
		vo.setScore(score);
		vo.setUserCode(userCode);
		
		RankingService service = new RankingService();
		List<RankingVO> list = service.test(vo);
		
		JSONArray jsonArray = JSONArray.fromObject(list);
		
		ModelAndView mav = new ModelAndView();
		mav.setViewName("Test");
		mav.addObject("test", jsonArray.toString().replace("[", "").replace("]", ""));
		
		return mav;
	}
	
	@RequestMapping(value = "/ranking/")
	public ModelAndView ranking(Locale locale, HttpServletRequest request, Model model) {
		
		String userCode = request.getParameter("userCode");
		
		if(userCode == null){
			userCode = "";
		}
		
		RankingService service = new RankingService();
		List<RankingVO> list = service.getRanking(userCode);
		
//		HashMap<String, String> jsonMap = new HashMap<String, String>();
//		jsonMap.put("NICKNAME", sUserCode);
//		JSONArray jsonArray = JSONArray.fromObject(jsonMap);

		JSONArray jsonArray = JSONArray.fromObject(list);
		
		ModelAndView mav = new ModelAndView();
		mav.setViewName("Ranking");
		mav.addObject("result", jsonArray.toString().replace("[", "").replace("]", ""));
		
		
		return mav;
	}
	
	
	@RequestMapping(value = "/createUserCode/")
	public ModelAndView createUserCode(Locale locale, HttpServletRequest request, Model model) {
		
		RankingService service = new RankingService();
		List<RankingVO> list = service.createUserCode();
		
		JSONArray jsonArray = JSONArray.fromObject(list);
		
		ModelAndView mav = new ModelAndView();
		mav.setViewName("UserInfo");
		mav.addObject("result", jsonArray.toString().replace("[", "").replace("]", ""));
		
		
		return mav;
	}
	
	
	
	@RequestMapping(value = "/access/")
	public ModelAndView access(Locale locale, HttpServletRequest request, Model model) {
		String userCode = request.getParameter("userCode");
		
		if(userCode == null){
			userCode = "";
		}
		
		RankingService service = new RankingService();
		List<RankingVO> list = service.access(userCode);
		
		JSONArray jsonArray = JSONArray.fromObject(list);
		
		ModelAndView mav = new ModelAndView();
		mav.setViewName("UserInfo");
		mav.addObject("result", jsonArray.toString().replace("[", "").replace("]", ""));
		
		
		return mav;
	}
	
	@RequestMapping(value = "/registerScore/")
	public ModelAndView registerScore(Locale locale, HttpServletRequest request, Model model) {
		
		String userCode = request.getParameter("userCode");
		if(userCode == null){
			userCode = "";
		}
		
		int score = 0;
		if(request.getParameter("score") != null){
			score = Integer.parseInt(request.getParameter("score"));
		}
		
		RankingVO vo = new RankingVO();
		vo.setScore(score);
		vo.setUserCode(userCode);
		
		RankingService service = new RankingService();
		List<RankingVO> list = service.registerScore(vo);
		
		JSONArray jsonArray = JSONArray.fromObject(list);
		
		ModelAndView mav = new ModelAndView();
		mav.setViewName("Ranking");
		mav.addObject("result", jsonArray.toString().replace("[", "").replace("]", ""));
		
		return mav;
	}
	
}
